package com.zz.io;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test7 {
    public static void main(String args[]){
        try (
                //2.创建缓冲字符输入流管道与源文件接通
                BufferedReader br = new BufferedReader(new FileReader("opp-app1\\src\\data\\test3.txt"));
                //5.创建缓冲字符输出流管道与目标文件接通
                BufferedWriter bw = new BufferedWriter(new FileWriter("opp-app1\\src\\data\\test4.txt"));
                ){
            //目标：恢复出师表的顺序到新文件中
            //1.定义一个ArrayList集合存储每段内容
            List<String> data = new ArrayList <>();

            //3.按照行读取每段
            String line;
            while((line = br.readLine()) != null){
                data.add(line);
            }
            //4.对List集合中的每段内容，依次写出去到新文件中
            Collections.sort(data);
            System.out.println(data);

            //6.遍历List集合总的每个元素，用缓冲输出管道写出并换行
            for (String s:data) {
                bw.write(s);
                bw.newLine();

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
